<?php 
namespace app\api\controller;
use think\Controller;
use think\Db;
use app\user\model\User as UserModel;
use app\admin\model\Scenicspot as ScenicspotModel;
/**
 供应链商品相关
 */
class User extends Common
{

	//首页待领取优惠券
    protected  $page = 10;
    private $useinfo = '';
    public function __construct()
    {
        parent::__construct();
    }

    //更改信息  车牌信息
    public function setplate(){
        $data = input('post.');
        if(empty($data['plate']) || empty($data['province']) || empty($data['city']) || empty($data['area'])){
                //记录省市区
            return returnJson(400,'参数错误！');
        }
        //是否已经写过了 写过了 就不行了
        $uid = $this->u_id;
        $userinfo = UserModel::getInfo("id=".$uid,'license_plate');
        if($userinfo['license_plate'] != ''){
             return returnJson(400,'车牌填写过不能更改的！');
        }
        //分享码是否窜在呢 dd
        $pid = 0;
        if(!empty($data['share_num']) && $data['share_num'] != ''){
            $pid =  UserModel::where(['mobile'=>$data['share_num']])->value('id');
            if(empty($pid)){
                return returnJson(400,'非法操作！');
            }
        }
        $data['pid'] = $pid;
        //省市区？
        $data['province_id'] = db('china')->where(['name'=>$data['province']])->value('id');
        $data['city_id'] = db('china')->where(['name'=>$data['city']])->value('id');
        $data['area_id'] =db('china')->where(['name'=>$data['area']])->value('id');
        UserModel::where(['id'=>$uid])->field('license_plate,pid,province,city,area,province_id,city_id,area_id')->setField($data);
        return returnJson(200,'操作成功！');
    }


    //如果是申请成为商家
    public function cashshop(){
        $data = input('post.');
        $uid = $this->u_id;
        //首先 需要判断袄 如果我是商家 那么 不能再申请了
        $userinfo = $this->get_me($uid,'role');

        //申请的资料呢  景区、汽修快保、汽修钣喷、汽修救援、景区住宿、景区餐饮、景区营地
        if(empty($data['name']) || empty($data['pic_url']) || empty($data['address']) ||
        empty($data['province']) || empty($data['city'])  || empty($data['area']) || empty($data['mobile'])
        ){
            return returnJson(400,'参数错误');
        }
        $data['province_id'] = db('china')->where(['name'=>$data['province']])->value('id');
        $data['city_id'] = db('china')->where(['name'=>$data['city']])->value('id');
        $data['area_id'] =db('china')->where(['name'=>$data['area']])->value('id');
        if($data['type'] == '1' || $data['type'] == '2' || $data['type'] == '3' ) {
             if($userinfo['role'] == 8){
                  return returnJson(400,'您已是景区商家，不能再申请汽车服务商！');
            }

            //有没有之前认证过呢
            $renzhengxinx = db('shop')->field('id,flag')->where(['del' => 0, 'add_uid' => $uid])->find();
            $up = 0;
            if (!empty($renzhengxinx)) {
                $up = 1;
            }
            $shopdata = [];
            $shopdata['shopname'] = $data['name'];
            $shopdata['pic_url'] = $data['pic_url'];
            $shopdata['province'] = $data['province'];
            $shopdata['city'] = $data['city'];
            $shopdata['area'] = $data['area'];
            $shopdata['map_address'] = $data['map_address'];
            $shopdata['province_id'] = $data['province_id'];
            $shopdata['city_id'] = $data['city_id'];
            $shopdata['area_id'] = $data['area_id'];
            $shopdata['mobile'] = $data['mobile'];
            $shopdata['add_time'] = date('Y-m-d H:i:s');
            $shopdata['add_uid'] = $uid;
            if ($up == 1) {
                db('shop')->where(['add_uid' => $renzhengxinx['id']])->setField($shopdata);
            } else {
                db('shop')->insert($shopdata);
            }
            //修改和添加么
            return returnJson(200, '操作成功！');
        }else {
            if($userinfo['role'] == 7){
                return returnJson(400,'您已是汽车服务商，不能再申请景区商家！');
            }
            //有没有之前认证过呢
            $renzhengxinx = db('admin_periphery')->field('id,flag')->where(['del' => 0, 'add_uid' => $uid])->find();
            $up = 0;
            if (!empty($renzhengxinx)) {
                $up = 1;
            }
            $ctype = '0';
            switch ($data['type']){// 景区、汽修快保、汽修钣喷、汽修救援、景区住宿、景区餐饮、景区营地
                case  '0'://景区门票  ['餐饮娱乐','酒店住宿','露营地点','景区门票'],
                    $ctype = '3';//'景区门票'
                    break;
                case  '4':
                    $ctype = '1';
                    break;
                case  '5'://景区
                    $ctype = '0';
                    break;
                case  '6'://露营地点
                    $ctype = '2';
                    break;
            }

            $shopdata = [];
            $shopdata['title'] = $data['name'];
            $shopdata['pic'] = $data['pic_url'];
            $shopdata['province'] = $data['province'];
            $shopdata['city'] = $data['city'];
            $shopdata['area'] = $data['area'];
            $shopdata['map_address'] = $data['map_address'];
            $shopdata['province_id'] = $data['province_id'];
            $shopdata['city_id'] = $data['city_id'];
            $shopdata['area_id'] = $data['area_id'];
            $shopdata['mobile'] = $data['mobile'];
            $shopdata['add_time'] = date('Y-m-d H:i:s');
            $shopdata['add_uid'] = $uid;
            $shopdata['type'] = $ctype;
            //添加人么
            $useirnfo = $this->get_me($uid,'mobile,nickname');
            $shopdata['umobile'] = $useirnfo['mobile'];
            $shopdata['uname'] = $useirnfo['nickname'];
            if ($up == 1) {
                db('admin_periphery')->where(['add_uid' => $renzhengxinx['id']])->setField($shopdata);
            } else {
                db('admin_periphery')->insert($shopdata);
            }
            //修改和添加么
            return returnJson(200, '操作成功！');
            //修改和添加么 == ''
        }

    }

    //获取我输入的一些门店信息
    public function my_shop(){
        //先有权限 才有 对应的信息
        $data = input('post.');
        $uid = $this->u_id;
        //首先 需要判断袄 如果我是商家 那么 不能再申请了
        //查询我有没有提交对应的信息
        $xinxi = db('admin_periphery')->where(['add_uid' => $uid])->find();

        $url = config('weburl');
        if(empty($xinxi)){
            $xinxi = db('shop')->where(['add_uid' => $uid])->find();
            if(empty($xinxi)){
                return returnJson(400, '未找到相关信息！');
            }
            $name = $xinxi['shopname'];
            $type = $xinxi['shoptype'];
            $pic = $xinxi['pic_urls'];
            $pohone = $xinxi['mobile'];
            $ccccc = explode(',',$pic);
            $pic = $url.get_file_path($ccccc[0]);
            $ctype = $type+1;
        }else{
            $name = $xinxi['title'];
            $type = $xinxi['type'];
            $pic = $url.$xinxi['pic_top_url'];
            $pohone = $xinxi['phone'];
            //['餐饮娱乐','酒店住宿','露营地点','景区门票'],  景区0、汽修快保1、汽修钣喷2、汽修救援3、景区住宿4、景区餐饮5、景区营地6
            switch ($type){//0汽修快保、1汽修钣喷、2汽修救援
                case 0:
                    $ctype = '5';
                    break;
                case 1:
                    $ctype = '4';
                    break;
                case 2:
                    $ctype = '6';
                    break;
                case 3:
                    $ctype = '0';
                    break;
            }
        }
       $returndata['name'] = $name;
       $returndata['type'] = $ctype;
       $returndata['pic'] = $pic;
       $returndata['phone'] = $pohone;
       $returndata['address'] = $xinxi['map_address'];
       $returndata['province'] = $xinxi['province'];
       $returndata['city'] = $xinxi['city'];
       $returndata['area'] = $xinxi['area'];
        return returnJson(200, '获取成功！',$returndata);
    }


    public function icon(){
        $cc = post_upload();
        if($cc['code'] == '1'){
            return json(array('code'=>'1','msg'=>'图片:'.$cc['msg']));
        }

        $url = 'http://'.$_SERVER['HTTP_HOST'];
        $arr = array();
        foreach ($cc['msg'] as $k=>$v){
            $arr[] = $url.'/'.get_files_path($v);
        }

        return json(array('code'=>'0','msg'=>'获取成功','data'=>$arr));
    }

    //核销记录
    public function hexiaomsg(){
        $limit = $this->getlimit();
        $url = config('weburl');
        $data = input('post.');
        if(empty($data['type'])){
            return returnJson(400,'参数错误');
        }
        $map = ['dates','<',date('Ym')];
        if($data['type']>1){
            //Shangge yue
            $map = ['dates','>=',date('Ym')];
        }
        $oldmap[] = $map;
        $retdata = db('admin_msg')::where($oldmap)->field('id aid,title,concat("'.$url.'",pic_url) pic,concat(star_time,"—",end_time)bm_time,look_num,`desc`,month')->order('solid asc,id desc')->limit($limit)->select();
        $count =db('admin_msg')::where($oldmap)->count();
        return returnJson(200,'获取成功！',['totalnum'=>$count,'list'=>$retdata]);
    }



    //上传单张图片
    public function upimg(){
        $res=upOneimg(0, 'file', $size = 11548484, $path = '/uploads/apifile/','user');
        return json($res);
    }


}